/**
 * 
 */
package kr.pe.johnkim.dayve.activity;

import java.util.ArrayList;
import java.util.List;

import kr.pe.johnkim.dayve.provider.DayveGroup;
import kr.pe.johnkim.dayve.provider.DayveGroupSelectIconAdapter;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * My History 아이콘 선택 화면을 출력하는 클래스이다.
 * 
 * @author johnkim
 *
 */
public class DayveGroupSelectIconView extends Activity {

	public static String TAG = DayveGroupSelectIconView.class.getSimpleName();
	
	// Adapter class 설정.
	private DayveGroupSelectIconAdapter dayveGroupSelectIconAdapter;
	
	// 화면 출력용 group 목록
	private ArrayList<DayveGroup> groupViewList;
	
	// 호출한 부모 Activity Name.
	private String PARENT_ACTIVITY_NAME = "dayve_group_default";

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dayve_group_select_icon);
		
		Intent i = getIntent();
		
		PARENT_ACTIVITY_NAME = i.getStringExtra("parent_activity_name");
		
		// 화면에 출력하기 위한 리스트 생성.
		groupViewList = new ArrayList<DayveGroup>();
		
		// Grid 를 세팅 한다.
		setGroupViewList();

		// Group 어댑터 생성.
		dayveGroupSelectIconAdapter = new DayveGroupSelectIconAdapter(getApplicationContext(), groupViewList);
		
		GridView gridView = (GridView)findViewById(R.id.gridViewGroupSelectIcon);
		
		// Grid view 의 cursor 를 제공하기 위해서 어댑터를 세팅 한다.
		gridView.setAdapter(dayveGroupSelectIconAdapter);
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				// TODO Auto-generated method stub
				
				// 호출한 부모 activity 의 파라메터에 세팅한다.
				if(("DayveCategoryAnalysisView").equals(PARENT_ACTIVITY_NAME)) {
					DayveCategoryAnalysisView.categoryIconUrl = groupViewList.get(position).getIconUrl();
				} else if(("DayveCategoryRegisterView").equals(PARENT_ACTIVITY_NAME)) {
					DayveCategoryRegisterView.DAYVE_CATEGORY_ICON_URL = groupViewList.get(position).getIconUrl();
				}
				
				Log.i(TAG, "DAYVE_GROUP_ICON_NAME : " + groupViewList.get(position).getIconUrl());
				
				finish();
			}
		});
		
		gridView.setAlwaysDrawnWithCacheEnabled(true);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	/* 최상위 프로세스가 아닐 경우 프로세스를 종료 한다.
	 * 
	 * => Home Key 를 눌렀을때 프로세스를 종료 한다.
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		if(!isForegrdound()) {
			DayveCategoryAnalysisView.activity.finish();
			DayveCategoryView.activity.finish();
			System.exit(0);
		}
	}
	
	/**
	 * 현재 프로세스가 최상위로 실행중인지 검사 한다.
	 * 
	 * true : 최상위
	 * 
	 * @return
	 */
	public boolean isForegrdound() {
	    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	    List<RunningTaskInfo> list = am.getRunningTasks(1);
	    ComponentName cn = list.get(0).topActivity;
	    String name = cn.getPackageName();
	     
	    return name.indexOf(getPackageName()) > -1;
	}
	
	/**
	 * 화면에 출력하기 위한 Grid 를 세팅 한다.
	 * 
	 * Icon 이 추가되면 클래스를 계속 추가 한다.
	 */
	private void setGroupViewList() {
		
		DayveGroup dayveGroup1 = new DayveGroup();
		dayveGroup1.setIconUrl("dayve_group_food");
		groupViewList.add(dayveGroup1);
		
		DayveGroup dayveGroup2 = new DayveGroup();
		dayveGroup2.setIconUrl("dayve_group_drink");
		groupViewList.add(dayveGroup2);
		
		DayveGroup dayveGroup3 = new DayveGroup();
		dayveGroup3.setIconUrl("dayve_group_play");
		groupViewList.add(dayveGroup3);
		
		DayveGroup dayveGroup4 = new DayveGroup();
		dayveGroup4.setIconUrl("dayve_group_study");
		groupViewList.add(dayveGroup4);
		
		DayveGroup dayveGroup5 = new DayveGroup();
		dayveGroup5.setIconUrl("dayve_group_meeting");
		groupViewList.add(dayveGroup5);
		
		DayveGroup dayveGroup6 = new DayveGroup();
		dayveGroup6.setIconUrl("dayve_group_culture");
		groupViewList.add(dayveGroup6);
		
		DayveGroup dayveGroup7 = new DayveGroup();
		dayveGroup7.setIconUrl("dayve_group_school");
		groupViewList.add(dayveGroup7);
		
		DayveGroup dayveGroup8 = new DayveGroup();
		dayveGroup8.setIconUrl("dayve_group_company");
		groupViewList.add(dayveGroup8);
		
		DayveGroup dayveGroup9 = new DayveGroup();
		dayveGroup9.setIconUrl("dayve_group_home");
		groupViewList.add(dayveGroup9);
		
		DayveGroup dayveGroup10 = new DayveGroup();
		dayveGroup10.setIconUrl("dayve_group_exercise");
		groupViewList.add(dayveGroup10);
		
		DayveGroup dayveGroup11 = new DayveGroup();
		dayveGroup11.setIconUrl("dayve_group_health");
		groupViewList.add(dayveGroup11);
		
		DayveGroup dayveGroup12 = new DayveGroup();
		dayveGroup12.setIconUrl("dayve_group_time");
		groupViewList.add(dayveGroup12);
		
		DayveGroup dayveGroup13 = new DayveGroup();
		dayveGroup13.setIconUrl("dayve_group_beauty");
		groupViewList.add(dayveGroup13);
		
		DayveGroup dayveGroup14 = new DayveGroup();
		dayveGroup14.setIconUrl("dayve_group_shopping");
		groupViewList.add(dayveGroup14);
		
		DayveGroup dayveGroup15 = new DayveGroup();
		dayveGroup15.setIconUrl("dayve_group_0001");
		groupViewList.add(dayveGroup15);
		
		DayveGroup dayveGroup16 = new DayveGroup();
		dayveGroup16.setIconUrl("dayve_group_0002");
		groupViewList.add(dayveGroup16);
		
		DayveGroup dayveGroup17 = new DayveGroup();
		dayveGroup17.setIconUrl("dayve_group_0003");
		groupViewList.add(dayveGroup17);
		
		DayveGroup dayveGroup18 = new DayveGroup();
		dayveGroup18.setIconUrl("dayve_group_0004");
		groupViewList.add(dayveGroup18);
		
		DayveGroup dayveGroup19 = new DayveGroup();
		dayveGroup19.setIconUrl("dayve_group_0005");
		groupViewList.add(dayveGroup19);
		
		DayveGroup dayveGroup20 = new DayveGroup();
		dayveGroup20.setIconUrl("dayve_group_0006");
		groupViewList.add(dayveGroup20);
		
		DayveGroup dayveGroup21 = new DayveGroup();
		dayveGroup21.setIconUrl("dayve_group_0007");
		groupViewList.add(dayveGroup21);
		
		DayveGroup dayveGroup22 = new DayveGroup();
		dayveGroup22.setIconUrl("dayve_group_0008");
		groupViewList.add(dayveGroup22);
		
		DayveGroup dayveGroup23 = new DayveGroup();
		dayveGroup23.setIconUrl("dayve_group_0009");
		groupViewList.add(dayveGroup23);
		
		DayveGroup dayveGroup24 = new DayveGroup();
		dayveGroup24.setIconUrl("dayve_group_0010");
		groupViewList.add(dayveGroup24);
		
		DayveGroup dayveGroup25 = new DayveGroup();
		dayveGroup25.setIconUrl("dayve_group_0011");
		groupViewList.add(dayveGroup25);
		
		DayveGroup dayveGroup26 = new DayveGroup();
		dayveGroup26.setIconUrl("dayve_group_0012");
		groupViewList.add(dayveGroup26);
		
		DayveGroup dayveGroup27 = new DayveGroup();
		dayveGroup27.setIconUrl("dayve_group_0013");
		groupViewList.add(dayveGroup27);
		
		DayveGroup dayveGroup28 = new DayveGroup();
		dayveGroup28.setIconUrl("dayve_group_0014");
		groupViewList.add(dayveGroup28);
		
		DayveGroup dayveGroup29 = new DayveGroup();
		dayveGroup29.setIconUrl("dayve_group_0015");
		groupViewList.add(dayveGroup29);
		
		DayveGroup dayveGroup30 = new DayveGroup();
		dayveGroup30.setIconUrl("dayve_group_0016");
		groupViewList.add(dayveGroup30);
	}
	
}
